<!--{template lyear:header_simple_start}-->
<link rel="stylesheet" href="static/lyear/js/layui/css/layui.css">
<style>
	.icon {
		width: 30px;
		height: 30px;
	}
	.iconFirstWord {
		width: 18px;
		height: 18px;
		line-height: 16px;
		font-size: 12px;
	}
</style>
<!--{template lyear:header_simple_end}-->
<script type="text/javascript">
	var selorg={};
	selorg.add=function(ctrlid,vals){
		if(vals[0].orgid=='other') vals[0].path='{lang please_select_a_organization_or_department}';
		jQuery('#'+ctrlid+'_Menu').html(vals[0].path+' ');
		jQuery('#sel_'+ctrlid).val(vals[0].orgid);
	}
</script>
<main class="bs-main-container">
	<div class="container-fluid">
		<div class="card">
			<div class="card-body">
				<div class="btn-group" id="breadcrumb"><a href="javascript:;" class="btn btn-primary fid-btn">{lang all_typename_attach}</a></div>
			</div>
		</div>
		<div class="card">
			<div class="card-body">
				<div class="card-search mb-2-5">
					  <div class="row">
						<div class="col-md-4">
						  <div class="row">
							<label class="col-sm-4 col-form-label">名称</label>
							<div class="col-sm-8">
							  <input type="text" class="form-control pull-left" name="keyword" id="keyword" placeholder="{lang filemanage_file_user}">
							</div>
						  </div>
						</div>
						<div class="col-md-4">
						  <div class="row">
							<div class="dropdown org">
								<input id="sel_orgid_{$org[orgid]}" type="hidden" name="orgid"  value="$org[orgid]" onchange="selDepart(this)" />
								<button type="button" id="orgid_{$org[orgid]}_Menu" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown"> $org[depart]  </button>
								<div id="orgid_$orgid_dropdown_menu" class="dropdown-menu org-sel-box" role="menu" aria-labelledby="orgid_{$org[orgid]}_Menu">
								<iframe name="orgid_{$org[orgid]}_iframe" class="org-sel-box-iframe" src="index.php?mod=system&op=orgtree&template=1&ctrlid=orgid_{$org[orgid]}&nouser=1&range=0&stype=1&zero={eval echo urlencode('{lang unselect_a_organization_or_department}');}" frameborder="0" marginheight="0" marginwidth="0" width="100%" height="100%" allowtransparency="true" ></iframe>
								</div>
							</div>
						  </div>
						</div>
						<div class="col-md-4">
						  <button type="submit" class="btn btn-primary me-1" id="search-btn">搜索</button>
						  <button type="reset" class="btn btn-default" id="refreshsearch">重置</button>
						</div>
					  </div>
				  </div>
                <table class="layui-hide" id="table" lay-filter="table"></table>
				<script type="text/html" id="toolbar">
					<div class="layui-btn-container">
						<button type="button"  class="layui-btn layui-btn-normal" lay-event="getCheckData">获取选中行数据</button>
						<button type="button" class="layui-btn layui-btn-danger" lay-event="delete">{lang delete}</button>
						<button type="button" class="layui-btn layui-btn-normal" lay-event="get">刷新</button>
					</div>
				</script>
				<script type="text/html" id="barDemo">
				<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">更多</a>
				<a class="layui-btn layui-btn--warm layui-btn-xs" lay-event="view">预览</a>
				<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="download">下载</a>
				<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
				</script>
			</div>
		</div>
	</div>
</main>
<script src="static/lyear/js/layui/layui.js"></script>
<script>
	var type = '';
	layui.use('table', function(){
	  var table = layui.table;
	  table.render({
		elem: '#table',
		url: '{MOD_URL}&do=getinfo',
		toolbar: '#toolbar',
		title: '文件管理数据表格',
		autoSort: false,
		text: {
			none: '暂无相关数据'
		},
		page: {
		  limit: 20, // 默认显示20行
		  limits: [10, 20, 50, 100, 200, 500, 900] // 可选的行数
		},
		cellMinWidth: 80 //全局定义常规单元格的最小宽度
		,cols: [[
		{type:'checkbox', fixed: 'left'}
		  ,{field:'id', title: 'ID',width:80, fixed: 'left'}
		  <!--{if $_G['adminid']}-->,{field:'username', title: '{lang possessor}', sort: true, align: 'center',templet: function(d) {
          return d.username;
        }}<!--{/if}-->
		  ,{field:'name', title: '{lang filename}', align: 'center', sort: true,templet: function(d) {
          return d.name;
        }}
		  ,{field:'size', title: '{lang file_size}', align: 'center', sort: true}
		  ,{field:'type', title: '{lang file_type}', align: 'center', sort: true}
		  ,{field:'relpath', title: '{lang file_location}', align: 'left'}
		  ,{field:'dateline', title: '{lang add_time}', align: 'center', sort: true}
		  ,{title: '{lang operation}',width:240, align:'center', toolbar: '#barDemo', fixed: 'right'}
		]],
		initSort: {
			field: 'dateline', // 默认排序字段
			type: 'desc' // 默认排序方式
		}
	  });
	  //头工具栏事件
	table.on('toolbar(table)', function(obj){
		var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
		var data = checkStatus.data;
		switch(obj.event){
			case 'getCheckData':
				if (data.length === 0) {
					showmessage("请至少选择一行", 'danger', 3000, 1);
					return;
				}
				var template = '<div class="accordion">';
				data.forEach(function(item) {
					template += `<div class="accordion-item">`;
					template += `<h2 class="accordion-header" id="Open-heading${item.id}">`;
					template += `<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#Open-collapse${item.id}" aria-expanded="true" aria-controls="Open-collapse${item.id}">`;
					template += `{lang filename}：${item.name}`;
					template += `</button>`;
					template += `</h2>`;
					template += `<div id="Open-collapse${item.id}" class="accordion-collapse collapse show" aria-labelledby="Open-heading${item.id}">`;
					template += `<div class="accordion-body">`;
					template += `<p><strong>ID:</strong> ${item.id}</p>`;
					<!--{if $_G['adminid']}-->template += `<p><strong>{lang possessor}:</strong> ${item.username}</p>`;<!--{/if}-->
					template += `<p><strong>{lang file_size}:</strong> ${item.size}</p>`;
					template += `<p><strong>{lang file_type}:</strong> ${item.type}</p>`;
					template += `<p><strong>{lang file_location}:</strong> ${item.relpath}</p>`;
					template += `<p><strong>{lang add_time}:</strong> ${item.dateline}</p>`;
					<!--{if $_G['adminid']}-->
					if (item.FileUri) {
						template += `<p><strong>直链地址:</strong> <a href="${item.FileUri}" target="_blank">${item.FileUri}</a></p>`;
					}
					if (item.copys) {
						template += `<p><strong>引用:</strong> ${item.copys}</p>`;
					}
					if (item.isdelete !== undefined) {
						template += `<p><strong>是否在回收站:</strong> ${item.isdelete}</p>`;
					}
					if (item.downs) {
						template += `<p><strong>下载次数:</strong> ${item.downs}</p>`;
					}
					if (item.views) {
						template += `<p><strong>{lang view_count}:</strong> ${item.views}</p>`;
					}
					if (item.edits) {
						template += `<p><strong>修改次数:</strong> ${item.edits}</p>`;
					}
					<!--{/if}-->
					template += `</div>`;
					template += `</div>`;
					template += `</div>`;
				});
				template += '</div>';
				layer.alert(template, {
					title: '选中的文件数据',
					btn: ['确定']
				});
				break;
				case 'delete':
					if (data.length === 0) {
						showmessage("请至少选择一行", 'danger', 3000, 1);
						return;
					}
					var postData = "";
					$.each(data,function(i) {
						postData +=  this.rid;
						if (i < data.length - 1) {
							postData += ",";
						}
					});
					layer.confirm('您确定要删除这'+data.length+'个文件(如果是文件夹将包含其下的子文件都会被删除)吗？删除之后不可恢复！', {skin:'lyear-skin-danger'}, function(index){
						var loading = layer.load(0, { shade:  [0.1, '#000'] });
						jQuery.post('{MOD_URL}&do=delete', {'icoid': postData},function(json){
							layer.close(loading);
							layer.close(index);
							if(json['msg']=='success'){
								showmessage('删除成功', 'success', '3000', 1);
								table.reload('table');
							}else if(json['msg']){
								showmessage(json['msg'], 'danger', '3000', 1);
							} else {
								showmessage('{lang error}', 'danger', '3000', 1);
							}
							},'json')
							.fail(function() {
								layer.close(loading);
								layer.close(index);
								showmessage('服务器发生错误，请稍后再试', 'danger',3000,1);
							});
					});
				break;
				case 'get':
				layui.table.reload('table', {
					where: {type: type}, // 清空搜索参数
					page: {
						curr: 1 //重新从第 1 页开始
					}
				});
				break; 
			};
		});
	  //监听工具条
	  table.on('tool(table)', function(obj){
		var data = obj.data;
		if(obj.event === 'detail'){
			showDataTemplate(obj);
		} else if(obj.event === 'view'){
			if (obj.data.ftype == 'folder') {
				// 刷新表格并传递搜索参数
				table.reload('table', {
					where: { pfid: obj.data.oid},
					page: {
						curr: 1
					},
					done: function(res, curr, count) {
						if(res.breadcrumb) {
							$('#breadcrumb').html(res.breadcrumb);
						}
					}
				});
			} else {
				var url = 'share.php?a=view&s='+obj.data.dpath;
				window.open(url);
			}
		} else if(obj.event === 'download'){
			var url = DZZSCRIPT + '?mod=io&op=download&path='+obj.data.dpath;
			window.open(url);
		} else if(obj.event === 'del'){
			layer.confirm('{lang filemanage_del_file}', {skin:'lyear-skin-danger'}, function(index){
				var loading = layer.load(0, { shade:  [0.1, '#000'] });
				$.ajax({
					type: "post",
					url: "{MOD_URL}&do=delete",
					data: {icoid: obj.data.rid},
					dataType: 'json',
					success: function (res, status) {
						layer.close(loading);
						layer.close(index);
						if (res.msg == 'success') {
							obj.del();
							showmessage('删除成功', 'success',3000,1);
						} else {
							showmessage(res.msg, 'danger',3000,1);
						}
					},
					error: function () {
						layer.close(index);
						layer.close(loading);
						showmessage('服务器发生错误，请稍后再试', 'danger',3000,1);
					}
				})
			});
		}
	  });
	  //触发排序事件 
	table.on('sort(table)', function(obj){
		table.reload('table', {
			initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
			,where: {
				field: obj.field //排序字段
				,order: obj.type //排序方式
			}
		});
	});
	  table.on('rowDouble(table)', function(obj){
		showDataTemplate(obj);
	  });
		function showDataTemplate(obj) {
			var data = obj.data;
			var template = `
				<div>
					<p><strong>ID:</strong> ${data.id}</p>
					<p><strong>{lang filename}:</strong> ${data.name}</p>
					<!--{if $_G['adminid']}--><p><strong>{lang possessor}:</strong> ${data.username}</p><!--{/if}-->
					<p><strong>{lang file_size}:</strong> ${data.size}</p>
					<p><strong>{lang file_type}:</strong> ${data.type}</p>
					<p><strong>{lang file_location}:</strong> ${data.relpath}</p>
					<p><strong>{lang add_time}:</strong> ${data.dateline}</p>
				<!--{if $_G['adminid']}-->${data.FileUri ? `<p><strong>直链地址:</strong> <a href="${data.FileUri}" target="_blank">${data.FileUri}</a></p>` : ''}
				${data.copys ? `<p><strong>引用:</strong> ${data.copys}</p>` : ''}
				${data.isdelete !== undefined ? `<p><strong>是否在回收站:</strong> ${data.isdelete}</p>` : ''}
				${data.downs ? `<p><strong>下载次数:</strong> ${data.downs}</p>` : ''}
				${data.views ? `<p><strong>{lang view_count}:</strong> ${data.views}</p>` : ''}
				${data.edits ? `<p><strong>修改次数:</strong> ${data.edits}</p>` : ''}<!--{/if}-->
				</div>
			`;
	
			layer.alert(JSON.stringify(data), {
				title: `${data.name} 详情：`,
				content: template,
			});
	
		// 标注选中样式
		obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
		}
		// 搜索按钮功能
		$(document).on('click', '#search-btn', function() {
		// 获取搜索关键词
		var keyword = $('#keyword').val();
		// 刷新表格并传递搜索参数
		table.reload('table', {
		  where: { keyword: keyword,orgid:$('#sel_orgid_{$org[orgid]}').val() },
		  page: {
			curr: 1 //重新从第 1 页开始
		  }
		});
	  });
	  var pfid = '';
		$(document).on('click', '.fid-btn', function() {
			table.reload('table', {
				where: {pfid:pfid}, // 清空搜索参数
				page: {
					curr: 1 //重新从第 1 页开始
				}
			});
		});
	   // 重置按钮功能
	   $('#refreshsearch').click(function() {
		// 清空表单内容
        $('#sel_orgid_{$org[orgid]}').val('');
        $('#keyword').val('');
		// 重置按钮内容
		$('#orgid_{$org[orgid]}_Menu').text('$org[depart]');
		// 刷新表格
		table.reload('table', {
		  where: {type: type}, // 清空搜索参数
		  page: {
			curr: 1 //重新从第 1 页开始
		  }
		});
	  });
	  // 类型按钮功能
	  $(document).on('click', '.type-btn', function() {
		// 移除所有 .type-btn 的 active 类
		$('.type-btn').removeClass('active');
		// 为当前点击的按钮添加 active 类
		$(this).addClass('active');
		// 获取当前的 type 值
		operation = $(this).data('operation');
		type = $(this).data('type');
		$('#keyword').val('');
		// 刷新表格并传递 operation 参数
		table.reload('table', {
		  where: {type: type},
		  page: {
			curr: 1 //重新从第 1 页开始
		  }
		});
	  });
	});
</script>
<!--{template lyear:footer_simple}-->